﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using devtm.Configuration;
using System.Reflection;

namespace devtm.Editor.Configuration.Command
{
    public class CommandAddItemWithAction : ICommand
    {
        

        
        private IElementSection elementSection;
        private System.Configuration.ConfigurationElement configurationElement;
        
        private MethodInfo MethodRedo;
        private object itemRedo;
        
        private Action MethodUndo;
        private object itemUndo;

        private TypeChange Change;
        public CommandAddItemWithAction(IElementSection elementSection, System.Configuration.ConfigurationElement configurationElement, MethodInfo MethodRedo, object itemRedo, Action MethodUndo, object itemUndo, TypeChange change)
        {

            Change = change;
            this.elementSection = elementSection;
            this.configurationElement = configurationElement;

            this.MethodRedo = MethodRedo;
            this.itemRedo = itemRedo;
            
            this.MethodUndo = MethodUndo;
            this.itemUndo = itemUndo;

        }

        public void Undo()
        {

            if (Change == TypeChange.Add)
                (elementSection.ParentItem as ElementSection).Items.Remove(elementSection);
            else
                (elementSection.ParentItem as ElementSection).Items.Add(elementSection);

            //MethodUndo();
                //string j = (string)(itemUndo as MethodInfo).Invoke(configurationElement, new object[] { itemRedo });
                //MethodUndo.Invoke(configurationElement, new object[] { j });

        }

        public void Redo()
        {

            if (Change == TypeChange.Add)
                (elementSection.ParentItem as ElementSection).Items.Add(elementSection);
            else
                (elementSection.ParentItem as ElementSection).Items.Remove(elementSection);


            MethodRedo.Invoke(configurationElement, new object[] { itemRedo });

        }

    }
}
